﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommerce.ManagementBase.Common.Base.Common;
using MyCommerce.Presentation.Stores.Presenters;
using MyCommerce.Presentation.Stores.IViews;

namespace MyCommerce.Windows.Stores.Views
{
    public partial class SaleProductChanges : BaseForm, ISaleProductChanges
    {
        private SaleProductChangesPresenter pPresenter;

        public SaleProductChanges()
        {
            InitializeComponent();
            pPresenter = new SaleProductChangesPresenter(this);
        }

        #region ISaleProductChanges Members

        public long IdDealerStore 
        {
            get
            {
                return Context.DealerStoreId;
            }
        }

        public DateTime SelectedDate 
        {
            get
            {
                return Convert.ToDateTime(fieldDate.Value);
            }
        }

        public DataTable DtSalesOrders
        {
            set 
            {
                gridSaleOrders.DataSource = value;
                gridSaleOrders.DataBind();
            }
        }

        public DataTable DtSaleDetails
        {
            set 
            {
                gridOrderDetails.DataSource = value;
                gridOrderDetails.DataBind();
            }
        }

        public long IdSaleOrderSelected
        {
            get 
            {
                if (gridSaleOrders.ActiveRow != null
                    && gridSaleOrders.ActiveRow.Selected)
                    return Convert.ToInt64(gridSaleOrders.ActiveRow.Cells["IdSalesOrder"].Value);
                return 0;
            }
        }

        public void CancelView(bool withRestoreDate)
        {
            if (withRestoreDate)
                fieldDate.Value = System.DateTime.Today;
            fieldQuantityChange.Value = 0;
            if (gridSaleOrders.DataSource != null
                && gridSaleOrders.Rows.Count > 0)
                ((DataTable)gridSaleOrders.DataSource).Clear();
            gridSaleOrders.DataBind();
            if (gridOrderDetails.DataSource != null
                && gridOrderDetails.Rows.Count > 0)
            ((DataTable)gridOrderDetails.DataSource).Clear();
            gridOrderDetails.DataBind();
            gridOrderDetails.Enabled = true;
            gridSaleOrders.Enabled = true;
        }

        public void CLoseView(bool result)
        {
            if (result)
                Context.ShowInformationMessageBox("La devolución/cambio de productos se realizó con éxito", "Mensaje");
            Close();
        }

        public event EventHandler OnSave;
        public event EventHandler OnCancel;
        public event EventHandler OnLoadSales;
        public event EventHandler OnLoadDetails;

        #endregion

        private void ultraGrid1_DoubleClickRow(object sender, Infragistics.Win.UltraWinGrid.DoubleClickRowEventArgs e)
        {
            try
            {
                gridSaleOrders.Enabled = false;
                if (OnLoadDetails != null)
                    OnLoadDetails(this, new EventArgs());
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }

        private void ultraGrid2_DoubleClickRow(object sender, Infragistics.Win.UltraWinGrid.DoubleClickRowEventArgs e)
        {
            gridOrderDetails.Enabled = false;
        }

        private void ultraDateTimeEditor1_EditorButtonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
        {
            try
            {
                if (OnLoadSales != null)
                    OnLoadSales(this, new EventArgs());
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }

        private void ultraNumericEditor1_EditorButtonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
        {
            try
            {
                if (gridOrderDetails.ActiveRow != null
                    && gridOrderDetails.ActiveRow.Selected)
                {
                    int quantityTotal = Convert.ToInt32(gridOrderDetails.ActiveRow.Cells["OrderQuantity"].Value);
                    int changeQuantity = Convert.ToInt32(fieldQuantityChange.Value);
                    if (changeQuantity <= quantityTotal)
                    {
                        gridOrderDetails.ActiveRow.Cells["QuantityChange"].Value = changeQuantity;
                        gridOrderDetails.DataBind();
                        gridOrderDetails.Enabled = true;
                        fieldQuantityChange.Value = 0;
                    }
                    else
                        Context.ShowInformationMessageBox("La cantidad de elementos devueltos y/o cambiados no puede ser mayor a la cantidad de elementos total del producto.", "Mensaje");
                }
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }

        private void SaleProductChanges_Load(object sender, EventArgs e)
        {
            fieldDate.Value = System.DateTime.Today;
            fieldQuantityChange.Value = 0;
        }

        private void tsSaveButtonToolStrip_Click(object sender, EventArgs e)
        {
            try
            {
                if (OnSave != null)
                    OnSave(this, new EventArgs());
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }

        private void tsCloseButtonToolStrip_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void tsCancelButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (OnCancel != null)
                OnCancel(this, new EventArgs());
        }
    }
}
